﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Pool : Singleton<Pool> {
	
	//Caches block - true: is free, false: is in use
	public List<Block> blocksPool;

	public void Init (GameObject prefab, int size) {

		blocksPool = new List<Block>();
		
		//Instantiate object
		for(int i=0; i<size; i++) {
			GameObject blockObj = Instantiate(prefab) as GameObject;
			Block blockScr = blockObj.GetComponent<Block>();
			Release(blockScr);
		}
	}
	
	public Block Pick () {
		
		if(blocksPool.Count > 0) {
			Block block = blocksPool[0];
			blocksPool.RemoveAt(0);
			block.Pick();
			return block;
		}
		else
			return null;
	}
	
	public void Release (Block block) {
		block.transform.position = new Vector3(100000, 100000, 0);
		block.Release();
		blocksPool.Add(block);
	}
}